﻿#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion
using System;
using Cysharp.Threading.Tasks;
using UnityEngine;
using Object = UnityEngine.Object;

namespace Neatly.Load
{
    public class AssetFileLoader : Loader
    {
        protected override void Init(string path, bool sync = true)
        {
            var pathAtlasTp = NeatlyConfig.PATH_ATLAS_TP;
            IsAtlas = path.IndexOf(pathAtlasTp, StringComparison.Ordinal) != -1;
            if (IsAtlas)
            {
                AtlasName = path.Replace(pathAtlasTp, string.Empty).SubstringTo(".");
            }
            base.Init(path, sync);
        }

        protected override async UniTask<Loader> Load()
        {
            string m_Name = $"{NeatlyConfig.ASSETS_ROOT}/{Path}";
#if UNITY_EDITOR
            // await UniTask.WaitForSeconds(0.1f);
            ResultObject = UnityEditor.AssetDatabase.LoadAssetAtPath<Object>(m_Name);
#endif
            if (IsAtlas)
            {
                var gameObject = ResultObject as GameObject;
                AtlasInfo = gameObject.GetComponent<NAtlas>();
                AtlasInfo.Init();
            }
            return this;
        }
    }
}